﻿using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace IISAdmin
{
    public class ServerManager
    {
        
        internal string HostName { get; private set; }

        #region Constructors
        public ServerManager()
            : this("localhost")
        {
        }

        public ServerManager(string hostname)
        {
            this.HostName = hostname;
            this.Sites = new SiteCollection(this);
        }
        #endregion

        public SiteCollection Sites { get; set; }

        internal string ADsPath { get { return string.Format("IIS://{0}", this.HostName); } }

        #region Methods
        /// <summary>
        /// Saves the metabase to long-term storage.
        /// </summary>
        public void Backup(string BackupName)
        {
            using (DirectoryEntry iisRoot = new DirectoryEntry(this.ADsPath))
            {
                iisRoot.Invoke("Backup",
                    new object[]
                    {
                        BackupName, // location
                        IISFlags.MD_BACKUP_NEXT_VERSION, // version
                        IISFlags.MD_BACKUP_SAVE_FIRST | IISFlags.MD_BACKUP_FORCE_BACKUP // backupFlags
                    });
            }
        }

        /// <summary>
        /// Saves the metabase to long-term storage with password.
        /// </summary>
        public void Backup(string BackupName, string Password)
        {
            using (DirectoryEntry iisRoot = new DirectoryEntry(this.ADsPath))
            {
                iisRoot.Invoke("BackupWithPassword",
                    new object[]
                    {
                        BackupName, // location
                        IISFlags.MD_BACKUP_NEXT_VERSION, // version
                        IISFlags.MD_BACKUP_SAVE_FIRST | IISFlags.MD_BACKUP_FORCE_BACKUP, // backupFlags
                        Password // Password
                    });
            }
        }

        /// <summary>
        /// The DeleteBackup method deletes a metabase backup from a backup location.
        /// </summary>
        public void DeleteBackup(string BackupName)
        {
            DeleteBackup(BackupName, IISFlags.MD_BACKUP_HIGHEST_VERSION);
        }

        /// <summary>
        /// The DeleteBackup method deletes a metabase backup from a backup location.
        /// </summary>
        public void DeleteBackup(string BackupName, long lVersion)
        {
            using (DirectoryEntry iisRoot = new DirectoryEntry(this.ADsPath))
            {
                iisRoot.Invoke("DeleteBackup",
                    new object[]
                    {
                        BackupName, // location
                        lVersion // version
                    });
            }
        }

        /// <summary>
        /// Enumerates metabase backups in long-term storage.
        /// </summary>
        public void EnumBackups()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Exports a section of the IIS metabase to a file for importing to the same IIS server or a different IIS server. The export file does not include machine-specific or system-specific data such as certain paths, GUIDs, passwords, and accounts.
        /// </summary>
        public void Export()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Imports a section of an IIS metabase from an export file that has been created on the same IIS server or a different IIS server. The export file does not include machine-specific or system-specific data such as certain paths, GUIDs, passwords, and accounts.
        /// </summary>
        public void Import()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Restores a metabase backup from long-term storage.
        /// </summary>
        public void Restore()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Triggers IIS to create a set of history files (configuration data and schema) that back up the entire IIS metabase. History files can only be restored to the computer on which the files were created.
        /// </summary>
        public void SaveData()
        {
            throw new System.NotImplementedException();
        }
        #endregion


    }
}
